package com.hframe.utils;

import java.io.*;

public class FileUtil {
	// private static Logger log = Logger.getLogger(FileUtil.class);

	/**
	 * 保存字符串到文件
	 * 
	 * @param json
	 *            字符串
	 * @param path
	 *            文件路径
	 */
	public static void SaveString(String json, String path) {
		DataOutputStream out = null;
		BufferedOutputStream outBuffer = null;
		FileOutputStream outFile = null;

		try {
			outFile = new FileOutputStream(path);
			outBuffer = new BufferedOutputStream(outFile);
			out = new DataOutputStream(outBuffer);
			out.write(json.getBytes());
		} catch (IOException e) {

		} finally {
			try {
				if (out != null)
					out.close();
				if (outBuffer != null)
					outBuffer.close();
				if (outFile != null)
					outFile.close();
			} catch (IOException e) {

			}
		}
	}

	/**
	 * 从文件里读取字符串
	 * 
	 * @param path
	 *            文件路径
	 * @return 字符串
	 */
	public static String readString(String path) {
		InputStream in = null;
		byte[] tempbytes = null;
		try {
			in = new FileInputStream(path);
			tempbytes = new byte[in.available()];
			in.read(tempbytes);

		} catch (Exception e) {
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
			}
		}
		if (tempbytes == null)
			return "";

		return new String(tempbytes);
	}
}
